﻿using Mapster;

namespace Devonline.Entity;

/// <summary>
/// 身份证
/// </summary>
[DisplayName("身份证")]
public class IdCardViewModel : IdCardViewModel<string>, IViewModel, IEntitySet, IDateTimeRange
{
    /// <summary>
    /// 通用附件集合, NotMapped 用于记录实体对象上上传的附件
    /// </summary>
    public virtual ICollection<Attachment>? Attachments { get; set; }

    /// <summary>
    /// 定义与 IdCard 的强制类型转换
    /// </summary>
    /// <param name="model"></param>
    public static implicit operator IdCard(IdCardViewModel model)
    {
        var entitySet = new IdCard();
        var config = new TypeAdapterConfig();
        config.ForDestinationType<IdCard>().Ignore(x => x.Birthday).Ignore(x => x.Start).Ignore(x => x.End);
        entitySet = model.CopyTo<IdCard>(config);
        entitySet.Birthday = model.Birthday is not null ? DateOnly.FromDateTime(model.Birthday.Value) : default;
        entitySet.Start = model.Start is not null ? DateOnly.FromDateTime(model.Start.Value) : default;
        entitySet.End = model.End is not null ? DateOnly.FromDateTime(model.End.Value) : default;
        return entitySet;
    }

    /// <summary>
    /// 定义与 IdCardViewModel 的强制类型转换
    /// </summary>
    /// <param name="model"></param>
    public static implicit operator IdCardViewModel(IdCard entity)
    {
        var model = new IdCardViewModel();
        var config = new TypeAdapterConfig();
        config.ForDestinationType<IdCardViewModel>().Ignore(x => x.Birthday).Ignore(x => x.Start).Ignore(x => x.End);
        model = entity.CopyTo<IdCardViewModel>(config);
        model.Birthday = entity.Birthday is not null ? entity.Birthday.Value.ToDateTime() : default;
        model.Start = entity.Start is not null ? entity.Start.Value.ToDateTime() : default;
        model.End = entity.End is not null ? entity.End.Value.ToDateTime() : default;
        return model;
    }
}

/// <summary>
/// 身份证
/// </summary>
/// <typeparam name="TKey">主键类型</typeparam>
[DisplayName("身份证")]
public abstract class IdCardViewModel<TKey> : ViewModel<TKey>, IViewModel<TKey>, IEntitySet<TKey>, IDateTimeRange where TKey : IConvertible
{
    /// <summary>
    /// 姓名
    /// </summary>
    [DisplayName("姓名"), Required, MaxLength(256), Excel(Size = AppSettings.DEFAULT_EXCEL_COLUMN_WIDTH)]
    public virtual string Name { get; set; } = null!;
    /// <summary>
    /// 性别 Gender 枚举值
    /// </summary>
    [DisplayName("性别"), Excel(Size = AppSettings.DEFAULT_EXCEL_COLUMN_WIDTH)]
    public virtual Gender Gender { get; set; } = Gender.Male;
    /// <summary>
    /// 名族
    /// </summary>
    [DisplayName("民族"), Excel(Size = AppSettings.DEFAULT_EXCEL_COLUMN_WIDTH)]
    public virtual Nation Nation { get; set; } = Nation.Han;
    /// <summary>
    /// 出身日期
    /// </summary>
    [DisplayName("出生"), Excel(Format = AppSettings.DEFAULT_DATE_FORMAT, Size = AppSettings.DEFAULT_EXCEL_COLUMN_WIDTH)]
    public virtual DateTime? Birthday { get; set; }
    /// <summary>
    /// 住址
    /// </summary>
    [DisplayName("住址"), MaxLength(256), Excel]
    public virtual string? Address { get; set; }
    /// <summary>
    /// 身份证号码
    /// </summary>
    [DisplayName("公民身份号码"), MaxLength(256), Unique, Excel(Size = AppSettings.UNIT_TWO * AppSettings.UNIT_TEN)]
    public virtual string IdCode { get; set; } = null!;
    /// <summary>
    /// 签发机关
    /// </summary>
    [DisplayName("签发机关"), MaxLength(36), Excel(Size = AppSettings.UNIT_TWO * AppSettings.UNIT_TEN)]
    public virtual string? IssuedBy { get; set; }
    /// <summary>
    /// 有效期始
    /// </summary>
    [DisplayName("有效期始"), Excel(Format = AppSettings.DEFAULT_DATE_FORMAT, Size = AppSettings.DEFAULT_EXCEL_COLUMN_WIDTH)]
    public virtual DateTime? Start { get; set; }
    /// <summary>
    /// 有效期至
    /// </summary>
    [DisplayName("有效期至"), Excel(Format = AppSettings.DEFAULT_DATE_FORMAT, Size = AppSettings.DEFAULT_EXCEL_COLUMN_WIDTH)]
    public virtual DateTime? End { get; set; }
    /// <summary>
    /// 身份证头像照片
    /// </summary>
    [DisplayName("头像照片"), MaxLength(128), BusinessType(IsAttachment = true, Name = "IdCard.HeadImage"), Excel]
    public virtual string? HeadImage { get; set; }
    /// <summary>
    /// 身份证正面照片
    /// </summary>
    [DisplayName("身份证正面照片"), MaxLength(128), BusinessType(IsAttachment = true, Name = "IdCard.FrontImage"), Excel]
    public virtual string? FrontImage { get; set; }
    /// <summary>
    /// 身份证反面照片
    /// </summary>
    [DisplayName("身份证反面照片"), MaxLength(128), BusinessType(IsAttachment = true, Name = "IdCard.BackImage"), Excel]
    public virtual string? BackImage { get; set; }
}